package com.rad.net.protocol.android.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;


/** 
 * 类说明：   配置信息工具类
 * @author  xinhui.cheng
 * @date    2014-8-27
 * @version 1.0
 */
public class CfgUtils {

	
	private static final String CFG_FILE_NAME = "cfg_rad_net";
	private static final String CFG_DEVICE_ID = "deviceId";
	
	private static Object obj = new Object();
	
	private CfgUtils(){}
	
	
	private static SharedPreferences getPreferences(Context context){
		return context.getSharedPreferences(CFG_FILE_NAME, Context.MODE_PRIVATE);
	}
	
	/**
	 * 保存设备ID
	 * @param context android上下文 
	 * @param deviceId 设备ID
	 */
	public static void saveDeviceId(Context context , String deviceId){
		synchronized (obj) {
			SharedPreferences sp = getPreferences(context);
			sp.edit().putString(CFG_DEVICE_ID, deviceId).commit();
		}
	}
	
	/**
	 * 获取设备ID
	 * @param context android上下文
	 * @return 设备ID字符串
	 */
	public static String getDeviceId(Context context){
		synchronized (obj) {
			SharedPreferences sp = getPreferences(context);
			return sp.getString(CFG_DEVICE_ID, null);
		}
	}
	
	/**
	 * 获取设备ID
	 * @param context android上下文
	 * @return 设备ID字符串
	 */
	public static String getAndroidId(Context context){
		return android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); 
	}
	
	/**
	 * 是否已经自动注册
	 * @param context android上下文
	 * @return true 已经自动注册，false 还没自动注册
	 */
	public static boolean hasAutoRegister(Context context){
		synchronized (obj) {
            return !TextUtils.isEmpty(getDeviceId(context));
        }
	}
	
	/**
	 * 获取设备的IMEI
	 * 
	 * @param context android上下文
	 * @return IMEI码或者无法获取时返回null
	 */
	public static String getIMEI(Context context) {
		String IMEI = "";
		try {
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			IMEI = tm.getDeviceId();

			if (IMEI != null
					&& (IMEI.length() < 15 || IMEI.equals("004999010640000")
							|| IMEI.equals("000000000000000") || IMEI
								.equals("null"))) {
				return null;
			}

			return IMEI;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取应用版本名
	 * @param cx android上下文
	 * @return 版本名或者无法获取时返回null
	 */
	public static String getVersionName(Context cx) {
		String packName = cx.getPackageName();
		PackageInfo pinfo = null;
		try {
			pinfo = cx.getPackageManager().getPackageInfo(packName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if (pinfo != null)
			return pinfo.versionName;
		else
			return null;
	}
	
	/**
	 * 获取应用版本号
	 * @param cx android上下文
	 * @return 版本号或者无法获取时返回-1
	 */
	public static int getVersionCode(Context cx) {
		String packName = cx.getPackageName();
		PackageInfo pinfo = null;
		try {
			pinfo = cx.getPackageManager().getPackageInfo(packName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if (pinfo != null)
			return pinfo.versionCode;
		else
			return -1;
	}

	/**
	 * 获取渠道号
	 * @param c  android上下文
	 * @return 渠道号或者无法获取是返回null
	 */
	public static String getChannelNumber(Context c) {
		try {
			ApplicationInfo ai = c.getPackageManager().getApplicationInfo(
					c.getPackageName(), PackageManager.GET_META_DATA);
			Bundle bundle = ai.metaData;
			String channelNumber = bundle.getString("CHANNEL");
			if(channelNumber == null){
				return "unknown";
			}
			return channelNumber;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "unknown";
	}

	/**
	 * 获取设备MAC地址
	 * @return 设备MAC地址
	 */
	public static String getLocalMacAddress(Context context) {
		WifiManager wifi = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}
	
}
